package domain;

import java.util.IllegalFormatConversionException;
import java.util.LinkedList;

import javax.swing.table.AbstractTableModel;

import com.audiobuddy.serializables.Song;


public class SongTableModel extends AbstractTableModel {
	
	private String[] columnNames = {"Nr","Title","Artist","Duration"};
	private LinkedList<Song> songs;

	public SongTableModel(){
		songs = new LinkedList<Song>();
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return songs.size();
	}
	
	

	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}

	@Override
	public Object getValueAt(int row, int col) {
		switch(col){
		case 0: return row+1;
		case 1: return songs.get(row).getTitle();
		case 2: return songs.get(row).getArtist();
		case 3: return getDuration(songs.get(row).getLength());
		}
		return null;
	}

	private String getDuration(long dur) {
		try{
			return String.format("%d:%02d", ((dur/1000000)%3600)/60, (dur/1000000)%60);
		}catch (IllegalFormatConversionException e) {
			return "..:..";
		}
	}

	public void addSong(Song song) {
		songs.add(song);
		fireTableDataChanged();
	}

	public void setSongs(LinkedList<Song> songs) {
		this.songs = songs;
		fireTableDataChanged();
		
	}

	public Song getSong(int row) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
